﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Huknow
{
    /// <summary>
    /// Interaction logic for TraceWindow.xaml
    /// </summary>
    public partial class TraceWindow : Window
    {
        public TraceWindow()
        {
            InitializeComponent();

            TextBlock block = new TextBlock(new Run("Waiting..."));
            block.Margin = new Thickness(10.0, 10.0, 0.0, 0.0);
            ScrollPlot.Children.Add(block);

            LoadTraceInformation();
        }

        private async void LoadTraceInformation()
        {
            Dictionary<IPAddress, List<Huknow.App.TracertEntry>> traceinfo = await (Application.Current as App).GetRemoteHostRouteInfo();
            ScrollPlot.Children.Clear();
            PreparePlotGrid(ScrollPlot);

            foreach (var pair in traceinfo)
            {
                Polyline poly = new Polyline();
                poly.Stroke = System.Windows.Media.Brushes.SlateGray;
                poly.StrokeThickness = 1;
                poly.FillRule = FillRule.EvenOdd;
                PointCollection pnts = new PointCollection();

                foreach (Huknow.App.TracertEntry trace in pair.Value)
                {
                    //TextBlock block = new TextBlock(new Run("><"));
                    //block.ToolTip = trace.ToString();
                    //block.Width = 10.0;
                    //block.Height = 10.0;
                    //block.Margin = new Thickness(50.0 * trace.HopID, 5.0 * trace.ReplyTime, 0.0, 0.0);
                    //ScrollPlot.Children.Add(block);
                    if (trace.ReplyStatus == System.Net.NetworkInformation.IPStatus.TimedOut)
                    {
                        pnts.Add(new Point(50.0 * trace.HopID, 0.0));
                    }
                    else
                    {
                        pnts.Add(new Point(50.0 * trace.HopID, (10.0 * trace.ReplyTime) + 50.0));
                    }
                }

                poly.Points = pnts;
                ScrollPlot.Children.Add(poly);
            }
            //TextBlock b = new TextBlock(new Run("Map of Interworld"));
            //b.Margin = new Thickness(0.0, 0.0, 0.0, 0.0);
            //ScrollPlot.Children.Add(b);
            DataPlot.ItemsSource = traceinfo;
        }

        private void PreparePlotGrid(Grid ScrollPlot)
        {
            Polyline poly = new Polyline();
            poly.Stroke = System.Windows.Media.Brushes.Green;
            poly.StrokeThickness = 0.5;
            poly.FillRule = FillRule.EvenOdd;
            PointCollection pnts = new PointCollection();

            for (int i = 0; i < 10; i++)
            {
                pnts.Add(new Point(5.0, 50.0 + 100.0 * i));
                pnts.Add(new Point(5.0, 100.0 + 100.0 * i));
                pnts.Add(new Point(1500.0, 100.0 + 100.0 * i));
                pnts.Add(new Point(1500.0, 150.0 + 100.0 * i));
                pnts.Add(new Point(5.0, 150.0 + 100.0 * i));
            }

            poly.Points = pnts;
            ScrollPlot.Children.Add(poly);
        }
    }
}
